package report

import (
	"../common"
	"../web"
	"strings"
)

type QueryBoxObj struct {
	common.BaseHandler
}

func (self *QueryBoxObj) Prepare() {}

func (self *QueryBoxObj) ParameterCheck() bool {
	boxid := self.Ctx.Params["q"]
	if boxid != "" {
		self.Sqlparas = append(self.Sqlparas, boxid)
		self.SqlQueryExecute()
		return false
	} else {
		boxid = strings.TrimSpace(self.Ctx.Params["boxid"])
	}
	if boxid == "" {
		return false
	}

	self.Sqlparas = append(self.Sqlparas, boxid)

	return true
}

func (self *QueryBoxObj) SqlQueryExecute() {
	box_main := *common.FetchOneRowPtr(
		`SELECT A.BOXID, A.BASEQTY, A.PACKEDQTY, 
				 CASE A.CLOSED WHEN 1 THEN 'Y' ELSE 'N' END, 
				 CASE A.ISDELIVERY WHEN 1 THEN 'Y' ELSE 'N' END,
				 ISNULL(B.PONO , '')
		   FROM SFC_BOX A(NOLOCK) 
		   LEFT JOIN SFC_DELIVERY B(NOLOCK)
			 ON A.BOXID=B.PRODUCTID
		  WHERE A.BOXID=?`,
		self.Usr.Conn, new(common.BoxMain), self.Sqlparas...)

	box_ls := *common.FetchAllRowsPtr(
		`SELECT ROW_NUMBER() OVER(ORDER BY SSN_NO),BOXID,SSN_NO,CONVERT(CHAR(23), MODIFYDATE, 121) 
           FROM SFC_BOXDETAIL(NOLOCK) 
          WHERE BOXID=?`,
		self.Usr.Conn, new(common.BoxInfo), self.Sqlparas...)

	self.TmplData["box_main"] = box_main
	self.TmplData["box_ls"] = box_ls
}

func QueryBoxHandler(ctx *web.Context) {
	self := new(QueryBoxObj)
	self.Ctx = ctx
	self.Html = "querybox.html"
	self.Rightlist = []string{"querybox"}
	self.DoQuery(self)
}
